<?php
	include('dbcon.php');
  $base_url = "http://localhost/gc/uv-nf/";
	
	/* 
	 * function checkValues
	 * 
	 * param $value
	 */
	function checkValues($value) {
		$value = trim($value);
		
		if (get_magic_quotes_gpc()) {
			$value = stripslashes($value);
		}
		
		$value = strtr($value,array_flip(get_html_translation_table(HTML_ENTITIES)));
		$value = strip_tags($value);
		$value = mysql_real_escape_string($value);
		$value = htmlspecialchars ($value);
		return $value;
	}
	
	/* 
	 * function clickable_link
	 * 
	 * param $text
	 */
	function clickable_link($text = '') {
		$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
		$ret = ' ' . $text;
		$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
		
		$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
		$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
		$ret = substr($ret, 1);
		return $ret;
	}
	
	/* 
	 * 
	 * 
	 * 
	 */
	function display_entry($row) {
		//echo "base url is " . base_url() . 'assets/';
		$base_url = "http://localhost/gc/uv-nf/";
		
		if ($row['type'] == "Status update") {
		  echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
			echo "<em> " . clickable_link($row['post']) . "</em>";
		} elseif ($row['type'] == "X and Y are now friends") {
		  echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
			echo "<em> and <font id=\"comment_name\">" . $row['extra1'] . "</font> are now friends.</em>";
		} elseif ($row['type'] == "X is now friends with Y and Z") {
		  echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
			echo "<em> is now friends with <font id=\"comment_name\">" . $row['extra1'] . "</font> and <font id=\"comment_name\">" . $row['extra2'] . "</font>.</em>";
		} elseif ($row['type'] == "You poke") {
		  echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
			echo "<em> poked <font id=\"comment_name\">" . $row['extra1'] . "</font>.</em>";
		} elseif ($row['type'] == "You are poked") {
			echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
			echo "<em> <font id=\"comment_name\">" . $row['extra1'] . "</font> poked you.</em>";
		} elseif ($row['type'] == "Wall message") {
			echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['extra1']."</b>";
      echo "<em> " . $row['extra2'] . "</em>";
		} elseif ($row['type'] == "X was tagged in an album") {
			echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
      echo "<em> was tagged in an album</em>";
		} elseif ($row['type'] == "X and Y are in a relationship") {
			echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
      echo "<em> and <font id=\"comment_name\">" . $row['extra1'] . "</font> are in a relationship.</em>";
		} elseif ($row['type'] == "X became a fan of Y") {
			echo "<img src=\"" . $base_url . "assets/img/".$row['f_image']."\" style=\"float:left;\" alt=\"\" />";
      echo "<label style=\"float:left\" class=\"name\">";
      echo "<b>".$row['f_name']."</b>";
      echo "<em> became a fan of <font id=\"comment_name\">" . $row['extra1'] . "</font></em>";
		} else {
			
		}
			
		echo "<br clear=\"all\" />";
		echo "<span>";

		// echo strtotime($row['date_created'],"Y-m-d H:i:s");
		$days = floor($row['TimeSpent'] / (60 * 60 * 24));
		$remainder = $row['TimeSpent'] % (60 * 60 * 24);
		$hours = floor($remainder / (60 * 60));
		$remainder = $remainder % (60 * 60);
		$minutes = floor($remainder / 60);
		$seconds = $remainder % 60;
		
		if($days > 0) {
			echo date('F d Y', $row['date_created']);
		} elseif($days == 0 && $hours == 0 && $minutes == 0) {
			echo "few seconds ago";
		} elseif($days == 0 && $hours == 0) {
		 echo $minutes.' minutes ago';
		} else {
			echo "few seconds ago";	
		}
		
		echo "</span>";
		echo "<a href=\"javascript: void(0)\" id=\"post_id".$row['p_id']."\" class=\"showCommentBox\">Comments</a>";
		echo "</label>";
	}
	/* END OF FUNCTIONS */
	
	
	
	$next_records = 10;
	$show_more_button = 0;
	
	if(isset($_REQUEST['value']) && checkValues($_REQUEST['value']) && checkValues($_REQUEST['type'])) { // adding a new base comment
		$userip = $_SERVER['REMOTE_ADDR'];
		$value = checkValues($_REQUEST['value']);
		$type = checkValues($_REQUEST['type']);
    $extra1 = checkValues($_REQUEST['extra1']);
    $extra2 = checkValues($_REQUEST['extra2']);
    $newsfeed_id = checkValues($_REQUEST['newsfeed_id']);
    
		mysql_query("INSERT INTO posts (nf_id, f_name, post, f_image, date_created, userip, type, extra1, extra2) VALUES('".$newsfeed_id."','99Points','".$value."','99.jpg','".strtotime(date("Y-m-d H:i:s"))."','".$userip."','".$type."','".$extra1."','".$extra2."')");
		
    echo "row inserted";
    
		$result = mysql_query("SELECT *, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM posts order by p_id desc limit 1");
	} elseif(isset($_REQUEST['show_more_post']) && $_REQUEST['show_more_post']) { // more posting paging
		$next_records = $_REQUEST['show_more_post'] + 10;
		
		$result = mysql_query("SELECT *, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM posts ORDER BY p_id DESC LIMIT ".$_REQUEST['show_more_post'].", 10");
		
		$check_res = mysql_query("SELECT * FROM posts ORDER BY p_id DESC LIMIT ".$next_records.", 10");
		$show_more_button = 0; // button in the end
		
		$check_result = mysql_num_rows(@$check_res);
		if($check_result > 0) {
			$show_more_button = 1;
		}
	} else { // default page load
		$show_more_button = 1;
		$result = mysql_query("SELECT *, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM posts WHERE nf_id = $newsfeed_id ORDER BY p_id DESC LIMIT 0,10");
	}
	
	// display posts with attached comments
	while ($row = mysql_fetch_array($result)) {
		$comments = mysql_query("SELECT *, UNIX_TIMESTAMP() - date_created AS CommentTimeSpent FROM posts_comments WHERE p_id = ".$row['p_id']." ORDER BY c_id ASC");
		?>
		<div class="friends_area" id="record-<?php echo $row['p_id']?>">
			<?php
				display_entry($row);
			?>
		
		<?php
			$userip = $_SERVER['REMOTE_ADDR'];
			if($row['userip'] == $userip) { ?>
				<a href="#" class="delete"> Remove</a>
				<?php
			} ?>
			<br clear="all" />
			<div id="CommentPosted<?php echo $row['p_id']?>">
				<?php
					$comment_num_row = mysql_num_rows(@$comments);
					if($comment_num_row > 0) {
						while ($rows = mysql_fetch_array($comments)) {
							$days2 = floor($rows['CommentTimeSpent'] / (60 * 60 * 24));
							$remainder = $rows['CommentTimeSpent'] % (60 * 60 * 24);
							$hours = floor($remainder / (60 * 60));
							$remainder = $remainder % (60 * 60);
							$minutes = floor($remainder / 60);
							$seconds = $remainder % 60;
							?>
							<div class="commentPanel" id="record-<?php echo $rows['c_id'];?>" align="left">
								<img src="<?php echo base_url(); ?>assets/img/speech_bubble.jpg" width="40" class="CommentImg" style="float:left;" alt="" />
								<label class="postedComments">
									<?php echo clickable_link($rows['comments']);?>
								</label>
								<br clear="all" />
								<span style="margin-left:43px; color:#666666; font-size:11px">
									<?php
										if($days2 > 0) {
											echo date('F d Y', $rows['date_created']);
										} elseif($days2 == 0 && $hours == 0 && $minutes == 0) {
											echo "few seconds ago";
										} elseif($days2 == 0 && $hours == 0) {
											echo $minutes.' minutes ago';
										} else {
											echo "few seconds ago";
										}
									?>
								</span>
								<?php
									$userip = $_SERVER['REMOTE_ADDR'];
									if($rows['userip'] == $userip) {
										?>&nbsp;&nbsp;<a href="#" id="CID-<?php  echo $rows['c_id'];?>" class="c_delete">Delete</a><?php
									} ?>
							</div>
							<?php
						}?>				
						<?php
					}?>
				</div>
				<div class="commentBox" align="right" id="commentBox-<?php  echo $row['p_id'];?>" <?php echo (($comment_num_row) ? '' :'style="display:none"')?>>
					<img src="<?php echo $base_url; ?>assets/img/speech_bubble.jpg" width="40" class="CommentImg" style="float:left;" alt="" />
					<label id="record-<?php  echo $row['p_id'];?>">
						<textarea class="commentMark" id="commentMark-<?php  echo $row['p_id'];?>" name="commentMark" cols="60"></textarea>
					</label>
					<br clear="all" />
					<a id="SubmitComment" class="small button comment"> Comment</a>
				</div>
		   </div>
		<?php
	}
	if($show_more_button == 1) { ?>
		<div id="bottomMoreButton">
			<a id="more_<?php echo @$next_records?>" class="more_records" href="javascript: void(0)">Older Posts</a>
		</div>
		<?php
	} ?>
